package com.cljz.makepassenger.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Selection;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseLazyFragment;
import com.cljz.makepassenger.model.net.bean.InviteListBean;
import com.cljz.makepassenger.present.fragment.SendOrReceiveInviteP;
import com.cljz.makepassenger.ui.activity.EditorInvitedActivity;
import com.cljz.makepassenger.ui.activity.InvitationLetterActivity;
import com.cljz.makepassenger.ui.adapter.SendOrReceiveInviteAdapter;
import com.cljz.makepassenger.widget.ClearEditText;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/27.
 */

public class SendOrReceiveInviteFragment extends BaseLazyFragment<SendOrReceiveInviteP> implements XRecyclerView.LoadingListener {
    private static final String LOAD_TAG = "MyInvite";
    @BindView(R.id.cet_search)
    ClearEditText cetSearch;
    @BindView(R.id.xrv_invite_list)
    XRecyclerView xrvInviteList;
    @BindView(R.id.ll_main)
    LinearLayout llMain;
    Unbinder unbinder;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    @BindView(R.id.btn_empty)
    Button btnEmpty;
    @BindView(R.id.rl_empty)
    RelativeLayout rlEmpty;
    @BindView(R.id.rl_invite_search)
    RelativeLayout rlInviteSearch;
    Unbinder unbinder1;
    private String type;
    private SendOrReceiveInviteAdapter sendOrReceiveInviteAdapter;
    private List<InviteListBean.DataBean> inviteList;
    private boolean isRefresh;
    private int page;
    private String key;
    private boolean isLoadMore;


    public static SendOrReceiveInviteFragment newInstance(String isInvite) {
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT, isInvite);
        SendOrReceiveInviteFragment sendOrReceiveInviteFragment = new SendOrReceiveInviteFragment();
        sendOrReceiveInviteFragment.setArguments(bundle);
        return sendOrReceiveInviteFragment;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        type = getArguments().getString(ARGUMENT);
        rlEmpty.setVisibility(View.GONE);
        tvEmpty.setText("send".equals(type) ? "您当前还未发布邀请" : "您当前还未接收邀请");
        btnEmpty.setText("发起邀请");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrvInviteList.setLayoutManager(linearLayoutManager);
        sendOrReceiveInviteAdapter = new SendOrReceiveInviteAdapter(getContext(), inviteList);
        xrvInviteList.setAdapter(sendOrReceiveInviteAdapter);
        xrvInviteList.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        xrvInviteList.setPullRefreshEnabled(true);
        xrvInviteList.setLoadingMoreEnabled(true);


    }

    @Override
    protected void initListener() {
        super.initListener();
        xrvInviteList.setLoadingListener(this);
//        xrvInviteList.refresh();
        RxView.clicks(rlInviteSearch)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(cetSearch, 0);
                    }
                });
        RxView.clicks(btnEmpty)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .putInt("type", EditorInvitedActivity.EDITOR_INVITE)
                                .to(EditorInvitedActivity.class)
                                .launch();
                    }
                });

        RxTextView.textChanges(cetSearch)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        Selection.setSelection(cetSearch.getText(), charSequence.length());
                        xrvInviteList.setNoMore(false);
                        if (charSequence.length() == 0) {
                            key = "";
                        } else {
                            key = charSequence.toString().trim();
                        }
                        xrvInviteList.refresh();
                    }
                });
        sendOrReceiveInviteAdapter.setOnItemClickListener(new SendOrReceiveInviteAdapter.OnItemClickListener() {
            @Override
            public void onClick(InviteListBean.DataBean dataBean) {
                Router.newIntent((Activity) App.getInstance()).to(InvitationLetterActivity.class).putInt("type", InvitationLetterActivity.INVITE).putString("id", dataBean.getInvite_id()).launch();
            }

            @Override
            public void onLongClick(InviteListBean.DataBean dataBean) {

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_invite_list;
    }

    @Override
    public SendOrReceiveInviteP newP() {
        return new SendOrReceiveInviteP();
    }


    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getP().getInviteList(page, type, key);
    }

    @Override
    public void onLoadMore() {
        isLoadMore = true;
        page++;
        getP().getInviteList(page, type, key);
    }

    public void getInviteListSucceed(List<InviteListBean.DataBean> dataBeanList) {
        dismissLoadingDialog();
        if (isLoadMore) {
            xrvInviteList.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                xrvInviteList.setNoMore(true);
            } else {
                sendOrReceiveInviteAdapter.addData(dataBeanList);
            }
            isLoadMore = false;
        } else if (isRefresh) {
            xrvInviteList.refreshComplete();
            inviteList = dataBeanList;
            if (inviteList.size() == 0) {
                rlEmpty.setVisibility(View.VISIBLE);
                xrvInviteList.setVisibility(View.GONE);
            } else {
                rlEmpty.setVisibility(View.GONE);
                xrvInviteList.setVisibility(View.VISIBLE);
                xrvInviteList.setNoMore(false);
                sendOrReceiveInviteAdapter.setData(dataBeanList);
            }
            isRefresh = false;
        } else {
            xrvInviteList.refreshComplete();
            inviteList = dataBeanList;
            sendOrReceiveInviteAdapter.setData(dataBeanList);
        }
    }

    public void getInviteListFail(String error) {
        xrvInviteList.refreshComplete();
        rlEmpty.setVisibility(View.VISIBLE);
        xrvInviteList.setVisibility(View.GONE);
    }

    public XRecyclerView getXrvInviteList() {
        return xrvInviteList;
    }

}
